package com.simdo.g768.Dialog;

import static com.simdo.g768.MainActivity.device;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Switch;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.g768.MessageControl.MessageController;
import com.simdo.g768.Adapter.DoRfAdapter;
import com.simdo.g768.Bean.DoRfBean;
import com.simdo.g768.Bean.object.Gnb_cfg;
import com.simdo.g768.Bean.object.Split_cfg1;
import com.simdo.g768.Bean.object.Split_cfg2;
import com.simdo.g768.Listener.CellClickListener;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class DoRfDialog extends Dialog {

    private final Context mContext;

    public DoRfDialog(Context context) {
        super(context, R.style.style_dialog);
        this.mContext = context;
        View root = View.inflate(context, R.layout.dialog_do_rf, null);
        this.setContentView(root);
        //this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = context.getResources().getDisplayMetrics().widthPixels * 6 / 7;// 设置宽度屏幕的 6 / 7
        lp.height = context.getResources().getDisplayMetrics().heightPixels * 3 / 4;
        window.setAttributes(lp);
        initView(root);
    }

    DoRfAdapter adapter;
    List<DoRfBean> list;
    Switch switch_rf;
    private void initView(View root) {
        switch_rf = root.findViewById(R.id.switch_rf); // 获取TextView视图
        boolean isCheck = false;
        for (int i = 0; i < 6; i++){
            if (device.getRfState(i) == 4){
                isCheck = true;
                break;
            }
        }
        switch_rf.setChecked(isCheck);
        switch_rf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switch_rf.isChecked()){
                    boolean isHasStart = false;

                    for (int i = 0; i < 6; i++) {
                        if (device.getDoState(i) != 2) continue;
                        isHasStart = true;
                        MessageController.build().openRF(device.getId(), i);
                        adapter.updateRFState(i, 1);
                    }

                    if(!isHasStart) {
                        MainActivity.getInstance().showToast(mContext.getString(R.string.open_cell_first));
                        switch_rf.setChecked(!switch_rf.isChecked());
                    }
                }else{
                    for (int i = 0; i < 6; i++) {
                        if (device.getDoState(i) != 2) continue;
                        MessageController.build().closeRF(device.getId(), i);
                        adapter.updateRFState(i, 2);
                    }
                }
            }
        });

        list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++){
            sb.setLength(0);
            DoRfBean bean = new DoRfBean();
            bean.setCellId(i);
            bean.setDoState(device.getDoState(i));
            bean.setRfState(device.getRfState(i));
            Gnb_cfg gnbCfg = device.getConfig(i).getGnb_cfg();
            
            if (gnbCfg.getDl_arfcn() != 0) {
                sb.append(gnbCfg.getDl_arfcn()).append("/").append(gnbCfg.getPci());
                Split_cfg1 splitCfg1 = gnbCfg.getSplit_cfg1();
                if (splitCfg1.getDl_arfcn() != 0) sb.append("\n").append(splitCfg1.getDl_arfcn()).append("/").append(splitCfg1.getPci());
                Split_cfg2 splitCfg2 = gnbCfg.getSplit_cfg2();
                if (splitCfg2.getDl_arfcn() != 0) sb.append("\n").append(splitCfg2.getDl_arfcn()).append("/").append(splitCfg2.getPci());
            }
            bean.setParam(sb.toString());
            list.add(bean);
        }
        RecyclerView rv_list = root.findViewById(R.id.rv_list); // 获取TextView视图
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new DoRfAdapter(list, new CellClickListener() {
            @Override
            public void onClick(int pos, int type) {
                DoRfBean bean = list.get(pos);
                int cellId = bean.getCellId();
                if (type == 0){
                    switch (bean.getDoState()){
                        case 0:
                            if (bean.getParam().isEmpty()){
                                MainActivity.getInstance().showToast(mContext.getString(R.string.no_cfg));
                                return;
                            }
                            MainActivity.getInstance().updateParamAndStart(cellId);
                            break;
                        case 2:
                            device.setDoState(cellId, 3);
                            freshDoAllUi(cellId);
                            MessageController.build().stopCell(device.getId(), cellId);
                            break;
                        case 1:
                        case 3:// 1、3状态不可点击，不会有回调
                            break;
                    }
                }else {
                    switch (bean.getRfState()) {
                        case 0:
                        case 1:
                        case 2:
                            // 0、1、2状态不可点击，不会有回调
                            break;
                        case 3:
                            MessageController.build().openRF(device.getId(), cellId);
                            device.setRfState(cellId, 1);
                            freshRFAllUi(cellId);
                            break;
                        case 4:
                            MessageController.build().closeRF(device.getId(), cellId);
                            device.setRfState(cellId, 2);
                            freshRFAllUi(cellId);
                            break;
                    }
                }
            }
        });
        rv_list.setAdapter(adapter);

        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss(); // 关闭对话框
            }
        });
    }

    private String getParam(int cell){
        StringBuilder sb = new StringBuilder();
        Gnb_cfg gnbCfg = device.getConfig(cell).getGnb_cfg();

        if (gnbCfg.getDl_arfcn() != 0) {
            sb.append(gnbCfg.getDl_arfcn()).append("/").append(gnbCfg.getPci());
            Split_cfg1 splitCfg1 = gnbCfg.getSplit_cfg1();
            if (splitCfg1.getDl_arfcn() != 0) sb.append("\n").append(splitCfg1.getDl_arfcn()).append("/").append(splitCfg1.getPci());
            Split_cfg2 splitCfg2 = gnbCfg.getSplit_cfg2();
            if (splitCfg2.getDl_arfcn() != 0) sb.append("\n").append(splitCfg2.getDl_arfcn()).append("/").append(splitCfg2.getPci());
        }
        return sb.toString();
    }

    public void freshDoAllUi(int cell) {
        list.get(cell).setParam(getParam(cell));
        adapter.updateDoState(cell, device.getDoState(cell));
    }

    public void freshRFAllUi(int cell) {
        adapter.updateRFState(cell, device.getRfState(cell));
        boolean isCheck = false;
        for (int i = 0; i < 6; i++){
            if (device.getRfState(i) == 4){
                isCheck = true;
                break;
            }
        }
        switch_rf.setChecked(isCheck);
    }
}
